/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.base.elements.method;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.api.attr.ChildAttribute;
import com.inspur.edp.cef.api.message.CefException;
import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.cef.gencommon.CefPropertyInfo;
import com.inspur.edp.cef.gencommon.ReflectionUtils;
import com.inspur.edp.cef.generator.entityGenerator.base.DataTypeGeneratorContext;
import com.inspur.edp.cef.generator.entityGenerator.base.elements.AssoPropertyGenerator;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import com.inspur.edp.udt.entity.ISimpleUdtData;
import lombok.var;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;

public class AssoSetValueGenerator extends ClassMethodGenerator {
    private static final String MethodName = "innerSetValue";
    private static final String param1 = "propName";
    private static final String param2 = "value";
    private ArrayList<ClassPropertyGenerator> propertyGenerators;
    private DataTypeGeneratorContext parentContext;

    public AssoSetValueGenerator(
            ArrayList<ClassPropertyGenerator> propertyGenerators,
            DataTypeGeneratorContext parentContext) {
        this.propertyGenerators = propertyGenerators;
        this.parentContext=parentContext;
    }

    @Override
    protected String getMethodName()
    {
        return MethodName;
    }

    @Override
    protected TypeInfo getReturnType() {
        return new TypeInfo(void.class.getTypeName());
    }

    @Override
    protected boolean getIsOverride() {
        return true;
    }

    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
        ArrayList<Modifier.ModifierKeyword> list =new ArrayList<>();
        list.add(Modifier.ModifierKeyword.PROTECTED_KEYWORD);
        return list;
    }
    @Override
    protected ArrayList<ParameterInfo> getParameterCollection() {
        ArrayList<ParameterInfo> list =new ArrayList<>();
        ParameterInfo parameterInfo=new ParameterInfo();
        parameterInfo.setParamType(new TypeInfo(String.class.getTypeName()));
        parameterInfo.setParamName(param1);
        list.add(parameterInfo);

        parameterInfo=new ParameterInfo();
        TypeInfo typeInfo=new TypeInfo(Object.class.getTypeName());
        parameterInfo.setParamType(typeInfo);
        parameterInfo.setParamName(param2);
        list.add(parameterInfo);
        return list;
    }
    @Override
    protected Block buildMethodBody() {
        Block block=ast.newBlock();
        SwitchStatement switchStatement=ast.newSwitchStatement();
        setSwitchExpresssion(switchStatement);
        setSwitchCases(switchStatement);
        setSwitchDefaultCase(switchStatement);
        block.statements().add(switchStatement);
        return block;
    }

    //region SwitchExpresssion
    private void setSwitchExpresssion(SwitchStatement switchStatement) {
        switchStatement.setExpression(ast.newSimpleName(param1));
    }
    //endregion


    //region SwitchCases
    private void setSwitchCases(SwitchStatement switchStatement) {
        for(ClassPropertyGenerator propertyInfo:propertyGenerators) {
            buildPropertyCaseStatement((AssoPropertyGenerator)propertyInfo,switchStatement);
        }
    }

    private void buildPropertyCaseStatement(AssoPropertyGenerator propertyInfo, SwitchStatement switchStatement) {

        SwitchCase switchCase1=ast.newSwitchCase();

        StringLiteral stringLiteral=ast.newStringLiteral();
        stringLiteral.setLiteralValue(propertyInfo.getPropertyName());
        switchCase1.setExpression(stringLiteral);
        switchStatement.statements().add(switchCase1);

        setSwitchCaseBody(switchStatement,propertyInfo);

        switchStatement.statements().add(ast.newBreakStatement());
    }

    private void setSwitchCaseBody(SwitchStatement switchStatement, AssoPropertyGenerator propertyInfo) {
//        if(propertyInfo.getElement().getIsUdt()) {
//            setUdtSwitchCaseBody(switchStatement,propertyInfo);
//        }
//        else
            setSimpleSwitchCaseBody(switchStatement, propertyInfo);
    }

    private void setSimpleSwitchCaseBody(SwitchStatement switchStatement, AssoPropertyGenerator propertyInfo) {
        MethodInvocation methodInvocation = ast.newMethodInvocation();
        methodInvocation.setName(ast.newSimpleName(propertyInfo.getSetterName()));

        CastExpression castExpression=ast.newCastExpression();
        TypeInfo info = propertyInfo.getType();
        castExpression.setType(info.getType(ast,parentContext.getEntityGenContext().getCefGenContext().getApiGenContext()));

        //非UDT枚举字段
        if(propertyInfo.getElement().getObjectType() == GspElementObjectType.Enum && !propertyInfo.getElement().getIsUdt()){
            addImport("com.inspur.edp.cef.spi.entity.ValueConvertUtils");
            var enumValueInvocation = ExpressionUtils.getMethodInvocation(ast, "ValueConvertUtils", "toEnum");

            var typeLiteral = ast.newTypeLiteral();
            typeLiteral.setType(propertyInfo.getType().getType(ast, parentContext.getEntityGenContext().getCefGenContext().getApiGenContext()));
            enumValueInvocation.arguments().add(typeLiteral);
            enumValueInvocation.arguments().add(ast.newSimpleName(param2));

            castExpression.setExpression(enumValueInvocation);

        }else{
            castExpression.setExpression(ast.newSimpleName(param2));
        }

        methodInvocation.arguments().add(castExpression);
        ExpressionStatement expressionStatementx=ast.newExpressionStatement(methodInvocation);
        switchStatement.statements().add(expressionStatementx);
    }

    private void setUdtSwitchCaseBody(SwitchStatement switchStatement, AssoPropertyGenerator propertyInfo)
    {
        IfStatement ifStatement=ast.newIfStatement();
        setUdtSwitchCaseIfExpression(ifStatement,propertyInfo);
        setUdtSwitchCaseIfThen(ifStatement,propertyInfo);
        setUdtSwitchCaseIfElse(ifStatement,propertyInfo);


        switchStatement.statements().add(ifStatement);
    }

    private void setUdtSwitchCaseIfExpression(IfStatement ifStatement, AssoPropertyGenerator propertyInfo) {
//        InfixExpression infixExpression1=ast.newInfixExpression();
//        infixExpression1.setOperator(InfixExpression.Operator.CONDITIONAL_AND);
//        InfixExpression infixExpression =ast.newInfixExpression();
//        infixExpression.setOperator(InfixExpression.Operator.NOT_EQUALS);
//
//        MethodInvocation methodInvocation=ast.newMethodInvocation();
//        methodInvocation.setName(ast.newSimpleName(propertyInfo.getGetterName()));
//        infixExpression.setLeftOperand(methodInvocation);
//        infixExpression.setRightOperand(ast.newNullLiteral());
//        infixExpression1.setLeftOperand(infixExpression);
//
//        Class udtItemType = (Class)ReflectionUtils.getGenericTypesOf(propertyType, ISimpleUdtData.class)[0];
//        InstanceofExpression instanceofExpression=ast.newInstanceofExpression();
//        instanceofExpression.setLeftOperand(ast.newSimpleName(param2));
//        instanceofExpression.setRightOperand(propertyInfo.getType().getType(ast, parentContext.getEntityGenContext().getCefGenContext().getApiGenContext()));
//        infixExpression1.setRightOperand(instanceofExpression);
//
//        ifStatement.setExpression(infixExpression1);
    }

    private void setUdtSwitchCaseIfThen(IfStatement ifStatement, AssoPropertyGenerator propertyInfo) {
//        Block block=ast.newBlock();
//        MethodInvocation methodInvocation=ast.newMethodInvocation();
//
//        MethodInvocation methodInvocation1=ast.newMethodInvocation();
//        methodInvocation1.setName(ast.newSimpleName(propertyInfo.getGetterName()));
//        methodInvocation.setExpression(methodInvocation1);
//
//        methodInvocation.setName(ast.newSimpleName("setValue"));
//
//        CastExpression castExpression=ast.newCastExpression();
//        Class udtItemType =null;
//
//        udtItemType= (Class)ReflectionUtils.getGenericTypesOf(propertyType, ISimpleUdtData.class)[0];
//        castExpression.setType(getType(ast,udtItemType));
//        castExpression.setExpression(ast.newSimpleName(param2));
//        methodInvocation.arguments().add(castExpression);
//
//        ExpressionStatement expressionStatement=ast.newExpressionStatement(methodInvocation);
//        block.statements().add(expressionStatement);
//        ifStatement.setThenStatement(block);
    }

    private void setUdtSwitchCaseIfElse(IfStatement ifStatement, AssoPropertyGenerator propertyInfo)
    {
        Block block =ast.newBlock();
        MethodInvocation methodInvocation=ast.newMethodInvocation();
        methodInvocation.setName(ast.newSimpleName(propertyInfo.getSetterName()));

        CastExpression castExpression=ast.newCastExpression();
        castExpression.setType(propertyInfo.getType().getType(ast, parentContext.getEntityGenContext().getCefGenContext().getApiGenContext()));
        castExpression.setExpression(ast.newSimpleName(param2));
        methodInvocation.arguments().add(castExpression);

        ExpressionStatement expressionStatement=ast.newExpressionStatement(methodInvocation);
        block.statements().add(expressionStatement);
        ifStatement.setElseStatement(block);
    }

    //endregion

    //region SwitchDefaultCase
    private void setSwitchDefaultCase(SwitchStatement switchStatement) {
        SwitchCase defaultCase = ast.newSwitchCase();
        defaultCase.setExpression(null);
        switchStatement.statements().add(defaultCase);

        ThrowStatement throwStatement= GeneratorUtil.getThrowStatement(ast,"属性名不存在：",param1,true);
        addImport(CefException.class.getTypeName());
//        ClassInstanceCreation classInstanceCreation =ast.newClassInstanceCreation();
//
//        classInstanceCreation.setType(new TypeInfo(RuntimeException.class).getType(ast));
//
//
//        InfixExpression infixExpression =ast.newInfixExpression();
//        infixExpression.setOperator(InfixExpression.Operator.PLUS);
//        StringLiteral stringLiteral= ast.newStringLiteral();
//        stringLiteral.setLiteralValue("属性名不存在：");
//        infixExpression.setLeftOperand(stringLiteral);
//        infixExpression.setRightOperand(ast.newSimpleName(param1));
//        classInstanceCreation.arguments().add(infixExpression);
//
//        throwStatement.setExpression(classInstanceCreation);
        switchStatement.statements().add(throwStatement);
    }

    //endregion

    public static Type getType(AST ast,  Class udtItemType ){
        if(udtItemType.getTypeName().equals("byte[]")){
            return new com.inspur.edp.caf.generator.baseInfo.TypeInfo(byte[].class).getType(ast);
        }
        else {
            return ast.newSimpleType(ast.newName(udtItemType.getTypeName()));
        }

    }
}
